from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait


class BasePage():
	_driver = None
	_base_url = ""

	def __init__(self, driver: WebDriver = None):
		if driver is None:
			"""
			浏览器复用
			"""
			chrome_options = Options()
			chrome_options.debugger_address = "127.0.0.1:9333"
			self._driver = webdriver.Chrome(options=chrome_options)
			self._driver.implicitly_wait(5)
		else:
			self._driver = driver

		if self._base_url != "":
			self._driver.get(self._base_url)

	def find(self, by, location):
		return self._driver.find_element(by, location)

	def finds(self, by, location):
		return self._driver.find_elements(by, location)

	def wait_for_click(self, locator):          #locator是一个元组
		# 定义显式等待（显式等待针对一个元素）
		# WebDriverWait函数的参数有两个，第一个是WebDrive的实例“driver”，，第二个是超时时间“timeout”
		# unitl是“直到”的意思，该方法自带了许多条件，在此我们选择的条件目的是为了检测该元素是否可以点击，
		# unitl检测到元素可以被点击，则退出循环，否则等待10秒
		WebDriverWait(self._driver, 10).until(expected_conditions.element_to_be_clickable(locator))

	#改造显式等待，如果符合条件，则退出死循环until
	def wait_for_condition(self, condition):
		WebDriverWait(self._driver, 10).until(condition)
